<template>
  <div class="aui-card-list">
    <uni-list-query-card :queryShow="queryShow" @query-click="getDataList()" @click="(e)=>{this.queryShow=e}"
      @searchReset="searchReset()">
      <el-col :span="6">
        <el-form-item label="表名称">
          <el-input v-model="dataForm.tableName" placeholder="表名称" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="表备注">
          <el-input v-model="dataForm.tableComment" placeholder="表备注" clearable></el-input>
        </el-form-item>
      </el-col>
    </uni-list-query-card>
    <el-card shadow="never" class="aui-card-list-body">
      <vxe-toolbar :setting="{immediate:true}">
        <template v-slot:buttons>
          <el-button v-if="$hasPermission('dpd:dpdTable:save')" type="primary" @click="addOrUpdateHandle()">{{
            $t('add') }}</el-button>
          <el-button v-if="$hasPermission('dpd:dpdTable:delete')" type="danger" @click="deleteHandle()">{{
            $t('deleteBatch') }}</el-button>
        </template>
        <template v-slot:tools>
          <el-button icon="el-icon-refresh" @click="getDataList()" circle></el-button>
        </template>
      </vxe-toolbar>
      <div class="aui-card-list-body-table">
      <vxe-table ref="xTable" :loading="dataListLoading" class="vxe-table-element" auto-resize
        @select-change="dataListSelectionChangeHandleVex" @select-all="dataListSelectionAllChangeHandleVex"
        :data="dataList" max-height="auto">
        <vxe-table-column type="checkbox" align="center" width="60"></vxe-table-column>
        <vxe-table-column field="tableName" title="表名称">
        </vxe-table-column>
        <vxe-table-column field="tableComment" title="表备注">
        </vxe-table-column>
        <!-- <vxe-table-column field="isUpdate" title="是否更新">
          <template slot-scope="scope">
            {{ scope.row.isUpdate ? '是':'否'}}
          </template>
        </vxe-table-column> -->
        <vxe-table-column field="syncFlag" title="同步?" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.syncFlag ? 'success':'error'" size="mini" effect="dark">{{ scope.row.syncFlag ?
              '已同步':'未同步'}}</el-tag>
          </template>
        </vxe-table-column>
        <vxe-table-column field="ruleName" title="规则"></vxe-table-column>
        <vxe-table-column field="updater" title="更新者">
        </vxe-table-column>
        <vxe-table-column field="updateDate" title="更新时间" align="center" width="140">
        </vxe-table-column>
        <vxe-table-column title="handle" width="200" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('dpd:dpdTable:update')" type="text" size="small" icon="el-icon-edit"
              @click="addOrUpdateHandle(scope.row)">{{
              $t('update') }}</el-button>
            <el-button v-if="$hasPermission('dpd:dpdTable:delete')" type="text" size="small" icon="el-icon-delete"
              @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-dropdown>
              <span class="el-button el-button--text el-button--small">
                {{ $t('more')}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <el-button type="text" icon="el-icon-view" size="small" @click="infoHandle(scope.row,false)">{{
                    '查看SQL' }}
                  </el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="text" icon="el-icon-s-promotion" size="small" @click="infoHandle(scope.row,true)">{{
                    '执行SQL' }}
                  </el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </vxe-table-column>
      </vxe-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import mixinDpdCommonJs from './common/dpd-common'

export default {
  mixins: [mixinViewModule, mixinDpdCommonJs],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/dpd/dpdTable/page',
        getDataListIsPage: true,
        deleteURL: '/dpd/dpdTable',
        deleteIsBatch: true
      },
      dataForm: {
        tableName: '',
        tableComment: '',
        isUpdate: ''
      }
    }
  },
  methods: {
    // 异常信息
    infoHandle (row, flag) {
      let self = this
      let sql = '<pre>' + row.createSql + '</pre>'
      this.$alert(sql, row.tableName, {
        customClass: 'mod-sql-view-info',
        dangerouslyUseHTMLString: true,
        showCancelButton: flag,
        beforeClose: async function (action, instance, done) {
          if (flag && action === 'confirm') {
            await self.createSQL(row)
            await self.getDataList()
          }
          done()
        }
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (row) {
      let title = '新增'
      let id
      if (row) {
        title = '编辑'
        id = row.id
      }
      // 打开路由
      var router = {
        name: 'dpd-dpdTable-editForm',
        id: id,
        menuId: this.$route.meta.menuId,
        oldName: 'dpd-dpdTable',
        path: 'dpd/dpdTable-editForm',
        title: title + '-' + (row ? row.tableComment : '表')
      }
      var params = {
        timestamp: this.$xeutils.timestamp(),
        id: id
      }
      this.$options.methods.openNewTab(this.$router, router, params)
    }
  }
}
</script>

<style lang="scss">
.el-message-box.mod-sql-view-info {
  width: 80%;
  height: 600px;
}
.mod-sql-view-info .el-message-box__content {
  height: 510px;
  overflow-y: auto;
}
</style>
